#!/usr/bin/env python

import unittest
from bitsio import BitsIO
import bitsio

class TestBitsIO(unittest.TestCase):
    def test__shift(self):
        print('test shift')
        a = bitsio.shift(b'\xab\xcd',4)
        self.assertEquals(a,b'\x0a\xbc')

    def test_getitem(self):
        print('test getitem')
        bio = BitsIO(b'\xab\xcd\xef')
        for i in range(0,16):
            for j in range(i+1,17):
               # print(i,j)
                bso = bio[i:j]
               # print(hex(bso))
            #print(hex(bio))
             #   self.assertEquals(hex(bso),
             #                     hex((0xABCDEF&((1<<j)-1))>>i))#,
                                 # msg='{0},{1}'.format(i,j))
            #print(hex(BitsIO(bs)))
            #print(hex(0xABCD>>i))

    def test_unpack1(self):
        print('test_unpack1')
        bio = BitsIO(b'\x11\x22')
        self.assertEquals(bio.unpack1(), 0x1122)
        self.assertEquals(bio.unpack1(endian='little'), 0x2211)

    def test_read(self):
        print('test_read')
        bio = BitsIO(b'\xab\xcd\xef')
        self.assertEquals(bio.read(8),0xab)
        self.assertEquals(bio.read(8),0xcd)
        self.assertEquals(bio.read(8),0xef)
        bio.seek(0)
        for i in [0xa,0xb,0xc,0xd,0xe,0xf]:
            self.assertEquals(bio.read(4),i)
        bio.seek(0)
        for i in [2,2,2,3]:
            self.assertEquals(bio.read(2),i)
        bio.seek(0)
        for i in [5,2,7,4,6]:
            self.assertEquals(bio.read(3),i)
        bio.seek(0)
        for i in [0xabc, 0xdef]:
            self.assertEquals(bio.read(12),i)
        bio.seek(4)
        self.assertEquals(bio.read(16,endian='little'),0xdebc)

if __name__ == '__main__':
    unittest.main()
